package omq.panel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.sun.org.apache.xpath.internal.operations.Bool;

import omq.api.OMQConnection;
import omq.api.OMQMessage;
import omq.api.OMQQueue;
import omq.api.OMQSession;
import omq.screen.OmqTesterMainScreen;
import constant.OmqTesterClientConst;

public class OmqPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6289857834909373162L;
	
	protected JLabel infoLbl;
	protected OMQConnection conn;
	protected OMQSession sess;
	protected static Preferences myPreferences = null;
	
	protected String errorMsg;
	protected final String sender = "OMQClientTester";
	//A message that will be filled in the getResponseCode method
	protected String responseMsg = "<html><body>";
	
	public OmqPanel(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		initialize();
	}

	public OmqPanel(LayoutManager arg0) {
		super(arg0);
		initialize();
	}

	public OmqPanel(boolean arg0) {
		super(arg0);
		initialize();
	}

	public OmqPanel() {
		super();
		initialize();
	}
	
	private void initialize(){
		this.setBackground(Color.WHITE);
		setPreferredSize(new Dimension(620,200));
		
	}
	
	protected void initpref()
	{
		if (myPreferences == null)
		myPreferences = Preferences.userNodeForPackage(OmqTesterMainScreen.class);
	}

	public JLabel getInfoLbl() {
		if(infoLbl == null){
			infoLbl = new JLabel();
			infoLbl.setPreferredSize(new Dimension(450,150));
			infoLbl.setVerticalAlignment(JLabel.TOP);
			infoLbl.setHorizontalAlignment(JLabel.CENTER);
			infoLbl.setToolTipText("This label holds the information for the viewed ability.");
		}
		return infoLbl;
	}
	
	/**
	 * This method gets the title and returns it as an html format
	 * @param title
	 * @return
	 */
	public static String getTitle(String title){
		return "<center><h2 style='color:"+OmqTesterClientConst.GUITitleColor+";text-decoration:underline;font-size:14px;Font-name: arial;'>" + title + "</h2></center>";
	}

	public void waitForInfo(boolean wait){
		Cursor cur = null;
		if(wait){
			cur = new Cursor ( Cursor.WAIT_CURSOR ) ;
		}else{
			cur = new Cursor ( Cursor.DEFAULT_CURSOR ) ;
		}
		this.setCursor ( cur ) ;
	}


	protected String printErrorFromOMQ(String methodName, boolean hr)
	{
		String response = "<html><body>";
		response += "<center>";
		if (hr) response += "<hr>";
		response += "<h2 style='color:"+OmqTesterClientConst.GUIResponseFailedcolor+";font-size:10px;text-decoration:underline;Font-name: arial;width:450px;'>";
		response += methodName + " failed</h2><br>" ;
		response += "<p align='left' style='padding-left:40px;Font-name: arial;'>" +
				   "error code &nbsp;&nbsp;&nbsp;&nbsp;: " + conn.getLastErrorCode();
		response += "<br>error reason : "+  conn.getLastErrorDescription() ;
		response += "</p></body></html>";
		
		return response;
	}
	
	protected String printQueueProperties(OMQQueue queue)
	{
		 String response; 
		 response = "<html><body><center><hr><h2 style='color:"+OmqTesterClientConst.GUIResponseOKcolor+";font-size:10px;text-decoration:underline;Font-name: arial;width:450px;'>Queue properties retreive successful</h2>";
//		 response += "<p align='center' style='padding-top:0px;padding-left:40px;Font-name: arial;'>";
		 response += "<table style='padding-top:0px;'>";
		 response += printTableRow("queue name",queue.getName());
//		 response += printTableRow("queue id",queue.getId());
		 response += printTableRow("queue description",queue.getDescription());
		 response += printTableRow("queue type",queue.getType());
		 response += printTableRow("queue curr depth",queue.getDepth());
		 response += printTableRow("queue max depth",queue.getMax_depth());
		 response += printTableRow("queue receive enabled",queue.getReceive_enabled());
		 response += printTableRow("queue send enabled",queue.getSend_enabled());
		 response += printTableRow("queue visibility timeout",queue.getVisibility_timeout());
		 response += "</table>";
//		 response += "</p>";
		 response += "<br>";
		 response += "</body></html>";	
		
		return response;
	}	
	
	private String printTableRow(String name, String value)
	{
		if (value == null)
			value = "null";
		
		return "<tr> " +
		 "<td>" +
		 name + 
		 "</td>" +
		 "<td>" +
		 "=" + 
		 "</td>" +
		 "<td>" +
		 value +  
		 "</td>" +
		 "</tr>";
	}
	private String printTableRow(String name, Boolean value)
	{
		if (value != null)
			return printTableRow(name, value.toString());
		else
			return printTableRow(name, "null");		
	}
	private String printTableRow(String name, Integer value)
	{
		if (value != null)
			return printTableRow(name, value.toString());
		else
			return printTableRow(name, "null");
	}

}
